clear			all
set 			more off
cd 				"~/Dropbox/Indonesia Migration/" // Specify your root directory here
/*******************************************************************************
				Effect on population size
********************************************************************************/
foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year geolev2 native oldmig perwt 
label 			var geolev2 "Residence district at the time of the survey"
bys				geolev2: egen pop_it = total(perwt) // Resident population of each province
bys				geolev2: egen popnative_it = total(perwt*native) // Resident population of each province
bys				geolev2: egen popoldmig_it = total(perwt*oldmig) // Resident population of each province
foreach 		i in pop_it popnative_it popoldmig_it {
gen				ln`i'= ln(`i')
}
keep			lnpop_it lnpopnative_it lnpopoldmig_it year geolev2
duplicates		drop geolev2 year, force
tempfile		a_`t'_pop_ict			
save			`a_`t'_pop_ict' // origin-destination pair-level survey year files	
}
use				`a_1976_pop_ict', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `a_`t'_pop_ict'
}
merge			1:1 geolev2 year using "Data/Merged"
drop			_merge
drop 			if missing(lnpop_it,lnpopnative_it,lnpopoldmig_it)

label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_it "log of district population"
label 			var  lnpopnative_it "log population of natives"
label 			var  lnpopoldmig_it "log population of old migrants"

/*******************************************************************************
				Conventional shift-share
********************************************************************************/
ivreg2			lnpop_it (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_1.xlsx", excel tex replace dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)
				
ivreg2			lnpopnative_it (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_1.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)			

ivreg2			lnpopoldmig_it (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_1.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)		

/*******************************************************************************
				Shift-share with 10-year lag
********************************************************************************/
ivreg2			lnpop_it (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_2.xlsx", excel tex replace dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)
				
ivreg2			lnpopnative_it (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_2.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)			

ivreg2			lnpopoldmig_it (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_2.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)	

/*******************************************************************************
				Multiple instruments
********************************************************************************/
ivreg2			lnpop_it (per_immig_cst_5Y per_immig_cst_5Y_lag = ///
				pred_per_immig_cst_Jaeger_10Y pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
mat				f=e(first)
local			SWF1 = f[8,1]
local			SWF2 = f[8,2]
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)	
outreg2			using "Tables/EDCC/Outmigration_3.xlsx", excel tex replace dec(4) ///
				keep(per_immig_cst_5Y per_immig_cst_5Y_lag) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat 1", `: di %9.2f `SWF1'', ///
				"Sanderson-Windmeijer F-Stat 2", `: di %9.2f `SWF2'', ///
				"Kleibergen-Paap rk LM statistic", `: di %9.2f `e(idstat)'', ///
				"p-value", "[`: di %9.2f `e(idp)'']", ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)				

ivreg2			lnpopnative_it (per_immig_cst_5Y per_immig_cst_5Y_lag = ///
				pred_per_immig_cst_Jaeger_10Y pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct , cl(geolev1) first	
mat				f=e(first)
local			SWF1 = f[8,1]
local			SWF2 = f[8,2]
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)	
outreg2			using "Tables/EDCC/Outmigration_3.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y per_immig_cst_5Y_lag) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat 1", `: di %9.2f `SWF1'', ///
				"Sanderson-Windmeijer F-Stat 2", `: di %9.2f `SWF2'', ///
				"Kleibergen-Paap rk LM statistic", `: di %9.2f `e(idstat)'', ///
				"p-value", "[`: di %9.2f `e(idp)'']", ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)	
				
ivreg2			lnpopoldmig_it (per_immig_cst_5Y per_immig_cst_5Y_lag = ///
				pred_per_immig_cst_Jaeger_10Y pred_per_immig_cst_10Y_5Y) ///
				i.year#i.geolev1 i.geolev2 av_high_educ_ct prop_male_ct ///
				av_age_ct , cl(geolev1) first	
mat				f=e(first)
local			SWF1 = f[8,1]
local			SWF2 = f[8,2]
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)	
outreg2			using "Tables/EDCC/Outmigration_3.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y per_immig_cst_5Y_lag) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat 1", `: di %9.2f `SWF1'', ///
				"Sanderson-Windmeijer F-Stat 2", `: di %9.2f `SWF2'', ///
				"Kleibergen-Paap rk LM statistic", `: di %9.2f `e(idstat)'', ///
				"p-value", "[`: di %9.2f `e(idp)'']", ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)					
/*******************************************************************************
				Effect on outflows
********************************************************************************/
foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			year migstat bpl origin perwt 
keep 			if migstat == 1
gen 			displaced_native = origin == bpl
gen 			displaced_oldmig = displaced_native==0
bys				origin: egen pop_displacednative = total(perwt*displaced_native)
bys				origin: egen pop_displacedoldmig = total(perwt*displaced_oldmig)
drop			migstat bpl perwt
rename			origin geolev1
duplicates		drop geolev1, force
tempfile		outflow_`t'
save			`outflow_`t''
use				"Data/Census_`t'.dta", clear
bys				geolev1: egen pop_it = total(perwt) // Resident population of each province
duplicates		drop geolev1, force
merge 1:1		geolev1	using `outflow_`t''
gen 			lnpop_displacednative = ln(pop_displacednative)
gen 			lnpop_displacedoldmig = ln(pop_displacedoldmig)
keep			lnpop_displacednative lnpop_displacedoldmig year geolev1
tempfile		logoutflow_`t'
save			`logoutflow_`t''
}
use				`logoutflow_1976', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `logoutflow_`t''
}
merge			1:m geolev1 year using "Data/Merged province level"
drop			_merge
drop 			if missing(lnpop_displacednative,lnpop_displacedoldmig)
label 			var  per_immig_cst_5Y "new migrant share in district population in year t"
label 			var  per_immig_cst_5Y_lag "new migrant share in district population in year t-5"
label 			var  lnpop_displacednative "log population share of natives who migrated out"
label 			var  lnpop_displacedoldmig "log population share of old migrants who migrated out"


/*******************************************************************************
				Conventional shift-share
********************************************************************************/		
ivreg2			lnpop_displacednative (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_1.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)			

ivreg2			lnpop_displacedoldmig (per_immig_cst_5Y = pred_per_immig_cst_5Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_1.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)		

/*******************************************************************************
				Shift-share with 10-year lag
********************************************************************************/
ivreg2			lnpop_displacednative (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_2.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)			

ivreg2			lnpop_displacedoldmig (per_immig_cst_5Y = pred_per_immig_cst_10Y_5Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct ///
				av_age_ct, cl(geolev1) first
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)
outreg2			using "Tables/EDCC/Outmigration_2.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat", `: di %9.2f `e(widstat)'', ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)	
/*******************************************************************************
				Multiple instruments
********************************************************************************/
ivreg2			lnpop_displacednative (per_immig_cst_5Y per_immig_cst_5Y_lag =  ///
				pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct av_age_ct, ///
				cl(geolev1) first	
mat				f=e(first)
local			SWF1 = f[8,1]
local			SWF2 = f[8,2]
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)	
outreg2			using "Tables/EDCC/Outmigration_3.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y per_immig_cst_5Y_lag) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat 1", `: di %9.2f `SWF1'', ///
				"Sanderson-Windmeijer F-Stat 2", `: di %9.2f `SWF2'', ///
				"Kleibergen-Paap rk LM statistic", `: di %9.2f `e(idstat)'', ///
				"p-value", "[`: di %9.2f `e(idp)'']", ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)
			
ivreg2			lnpop_displacedoldmig (per_immig_cst_5Y per_immig_cst_5Y_lag =  ///
				pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) ///
				i.year i.geolev1 av_high_educ_ct prop_male_ct av_age_ct, ///
				cl(geolev1) first
mat				f=e(first)
local			SWF1 = f[8,1]
local			SWF2 = f[8,2]
su				`e(depvar)' if e(sample) == 1, d				
local			meandv=r(mean)	
outreg2			using "Tables/EDCC/Outmigration_3.xlsx", excel tex append dec(4) ///
				keep(per_immig_cst_5Y per_immig_cst_5Y_lag) label nocons nonotes ///
				addtext("Sanderson-Windmeijer F-Stat 1", `: di %9.2f `SWF1'', ///
				"Sanderson-Windmeijer F-Stat 2", `: di %9.2f `SWF2'', ///
				"Kleibergen-Paap rk LM statistic", `: di %9.2f `e(idstat)'', ///
				"p-value", "[`: di %9.2f `e(idp)'']", ///
				"Mean of DV", `: di %9.2f `meandv'', "# of provinces", ///
				`e(N_clust)', "District FE", YES, "Province-year FE", YES)
